package com.xindaoapp.framework.activity;

import java.text.DecimalFormat;
import java.util.LinkedList;



import java.util.List;

import com.alibaba.fastjson.JSON;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnLastItemVisibleListener;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.squareup.picasso.Picasso;
import com.xindaoapp.framework.R;
import com.xindaoapp.framework.activity.NewsActivity.ViewHolder;
import com.xindaoapp.framework.api.NewApiImpl;
import com.xindaoapp.framework.bean.Good;
import com.xindaoapp.framework.bean.SerachGood;
import com.xindaoapp.framework.utils.IRequest;

import android.content.Intent;
import android.graphics.Paint;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class CatListActivity extends BaseActivity {
	private String catID;
	private String catName;
	private PullToRefreshListView pull;
	private RelativeLayout no_result;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_refresh);
		if(!TextUtils.isEmpty(getIntent().getStringExtra("catID"))){
			catID=getIntent().getStringExtra("catID");
		}
		if(!TextUtils.isEmpty(catName=getIntent().getStringExtra("catName"))){
			catName=getIntent().getStringExtra("catName");
		}
		setTitle(catName);
		initCatList(page);
	}
	private NewApiImpl impl;
	private int page=1;
	private LinkedList<Good> goods=new LinkedList<Good>();
	private int total;
	private GoodSerachAdapter adapter;
	private void initCatList(int page2) {
		no_result=(RelativeLayout) findViewById(R.id.no_result);
		pull=(PullToRefreshListView) findViewById(R.id.pull);
		impl = NewApiImpl.getInstance();
		pull.setMode(Mode.DISABLED);
		pull.setOnLastItemVisibleListener(new OnLastItemVisibleListener() {
			@Override
			public void onLastItemVisible() {
				if(total==goods.size()){
					Toast.makeText(getApplicationContext(), "没有更多数据了", Toast.LENGTH_SHORT).show();
					pull.onRefreshComplete();            
				}else{
					getCats(++page);
				}
			}
		});
		pull.getRefreshableView().setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				try {
					
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
		getCats(page);
	}
	protected void getCats(int page) {
		impl.getCats(page, 10, catID, catName, new IRequest<String>() {
			@Override
			public void request(String t) {
				System.out.println(t);
				if(t.equals("error")){
					pull.setMode(Mode.DISABLED);
					pull.onRefreshComplete();
				}else{
					try {
						SerachGood parse = JSON.parseObject(t, SerachGood.class);
						if(parse.getStatus().equals("1")){
							total=Integer.parseInt(parse.data.count);
							if(!parse.getMsg().equals("暂无数据！")){
								if(total>0){
									pull.setVisibility(View.VISIBLE);
									List<Good> info = parse.data.info;
									goods.addAll(info);
									if(adapter==null){
										adapter=new GoodSerachAdapter(goods);
										pull.getRefreshableView().setAdapter(adapter);
									}else{
										adapter.notifyDataSetChanged();
									}
								}else{
									Toast.makeText(getApplicationContext(),parse.getMsg() , Toast.LENGTH_SHORT).show();
								}
							}else if(parse.getMsg().equals("暂无数据！")&&goods.size()<=0){
								no_result.setVisibility(View.VISIBLE);
								pull.setVisibility(View.GONE);
							}
						}
						if(total<=15){
							pull.setMode(Mode.DISABLED);
						}
						pull.onRefreshComplete();
					} catch (Exception e) {
						pull.onRefreshComplete();
						e.printStackTrace();
					}
				}
			}
		});
	}
	private class GoodSerachAdapter extends BaseAdapter {
		private final List<Good> goods2;
		public GoodSerachAdapter(List<Good> goods2) {
			super();
			this.goods2 = goods2;
		}

		@Override
		public int getCount() {
			return goods2.size();
		}

		@Override
		public Object getItem(int position) {
			return goods2.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder;
			if (convertView == null) {
				convertView = View.inflate(CatListActivity.this, R.layout.good_common_item, null);
				holder = new ViewHolder();
				holder.good_pic = (ImageView) convertView.findViewById(R.id.good_pic);
				holder.good_name = (TextView) convertView.findViewById(R.id.good_name);
				holder.good_shop_price = (TextView) convertView.findViewById(R.id.good_shop_price);
				holder.good_market_price = (TextView) convertView.findViewById(R.id.good_market_price);
				holder.good_sell = (TextView) convertView.findViewById(R.id.good_sell);
				holder.good_surplus = (TextView) convertView.findViewById(R.id.good_surplus);
				holder.goods_comment = (TextView) convertView.findViewById(R.id.goods_comment);
				holder.good_dazhe = (TextView) convertView.findViewById(R.id.good_dazhe);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
//			holder.good_name.setText(goods2.get(position).goods_name);
			holder.good_name.setText(goods2.get(position).goods_name);
			holder.good_shop_price.setText(formatPrice(Float.valueOf(goods2.get(position).shop_price)));
			holder.good_market_price.setText("￥"
					+ formatPrice(Float.valueOf(goods2.get(position).market_price)));
			holder.good_market_price.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
			holder.good_sell.setText(goods2.get(position).sold_number);
			holder.good_surplus.setText(goods2.get(position).goods_number);
			holder.goods_comment.setText(goods2.get(position).comment_percent);
			Picasso.with(CatListActivity.this).load(goods2.get(position).goods_thumb).into(holder.good_pic);
			float a = (Float.valueOf(goods2.get(position).shop_price) / Float
					.valueOf(goods2.get(position).market_price)) * 10;
			holder.good_dazhe.setText(formatDaZhe(a) + "折");
			return convertView;
		}
	}

	private String formatDaZhe(Float a) {
		DecimalFormat df = new DecimalFormat("0.0");
		return df.format(a);
	}

	private String formatPrice(Float a) {
		DecimalFormat df = new DecimalFormat("0.00");
		return df.format(a);
	}

	public class ViewHolder {
		ImageView good_pic;
		TextView good_name;
		TextView good_shop_price;
		TextView good_market_price;
		TextView good_sell;
		TextView good_surplus;
		TextView goods_comment;
		TextView good_dazhe;
	}
}
